package com.ydj.framework.third.ip;

import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.core.net.url.UrlQuery;
import cn.hutool.http.HttpUtil;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;

public class GetIpAddressManager {
    private static final String LBS_QQ_KEY = "";

    /**
     * 根据经纬，获取地址
     */
    public static String getAddressByLatLng(String longitude, String latitude) {
        // 文档：https://lbs.qq.com/service/webService/webServiceGuide/address/Gcoder
        String baseUrl = "https://apis.map.qq.com/ws/geocoder/v1/";
        String url = UrlBuilder.of(baseUrl)
                .setQuery(new UrlQuery()
                        .add("key", LBS_QQ_KEY)
                        // 文档是 location=lat<纬度>,lng<经度>
                        .add("location", latitude + "," + longitude)
                        .add("get_poi", 0))
                .build();
        try {
            String strRes = HttpUtil.get(url);
            ThirdGetAddressLatLngByTencentApiRes res = new Gson().fromJson(strRes, ThirdGetAddressLatLngByTencentApiRes.class);
            if (res.getStatus() != 0) {
                return "";
            }
            ThirdGetAddressLatLngByTencentApiRes.ResultBean resultBean = res.getResult();
            if (resultBean == null) {
                return "";
            }
            ThirdGetAddressLatLngByTencentApiRes.ResultBean.AdInfoBean adInfo = resultBean.getAd_info();
            if (adInfo == null) {
                return "";
            }
            String address = "";
            if (StringUtils.isNotBlank(adInfo.getProvince())) {
                address += adInfo.getProvince().replace(" ", "");
            }
            if (StringUtils.isNotBlank(adInfo.getCity())) {
                address += adInfo.getCity().replace(" ", "");
            }
            return address.trim();
        } catch (Exception e) {
            return "";
        }
    }

    public static String getIpAddress(String ip) {
        // 调用腾讯API
        String address = getIpAddressByTencentApi(ip);
        if (StringUtils.isNotBlank(address)) {
            return address;
        }
        // 调用百度的API
        address = getIpAddressByBaiduApi(ip);
        if (StringUtils.isNotBlank(address)) {
            return address;
        }
        return "";
    }

    private static String getIpAddressByTencentApi(String ip) {
        String baseUrl = "https://apis.map.qq.com/ws/location/v1/ip";
        String url = UrlBuilder.of(baseUrl)
                .setQuery(new UrlQuery()
                        .add("key", LBS_QQ_KEY)
                        .add("ip", ip))
                .build();
        try {
            String strRes = HttpUtil.get(url);
            ThirdGetIpAddressByTencentApiRes res = new Gson().fromJson(strRes, ThirdGetIpAddressByTencentApiRes.class);
            if (res.getStatus() != 0) {
                return "";
            }
            ThirdGetIpAddressByTencentApiRes.ResultBean resultBean = res.getResult();
            if (resultBean == null) {
                return "";
            }
            ThirdGetIpAddressByTencentApiRes.ResultBean.AdInfoBean adInfo = resultBean.getAd_info();
            if (adInfo == null) {
                return "";
            }
            String address = "";
            if (StringUtils.isNotBlank(adInfo.getProvince())) {
                address += adInfo.getProvince().replace(" ", "");
            }
            if (StringUtils.isNotBlank(adInfo.getCity())) {
                address += adInfo.getCity().replace(" ", "");
            }
            return address.trim();
        } catch (Exception e) {
            return "";
        }
    }

    // 基于百度的API
    public static String getIpAddressByBaiduApi(String ip) {
        String url = String.format("https://opendata.baidu.com/api.php?query=%s&co=&resource_id=6006&oe=utf8", ip);
        try {
            String strRes = HttpUtil.get(url);
            ThirdGetIpAddressByBaiduApiRes res = new Gson().fromJson(strRes, ThirdGetIpAddressByBaiduApiRes.class);
            if (!"0".equals(res.getStatus())) {
                return "";
            }
            if (res.getData() == null || res.getData().isEmpty()) {
                return "";
            }
            ThirdGetIpAddressByBaiduApiRes.DataBean dataBean = res.getData().get(0);
            String location = dataBean.getLocation();
            String address = "";
            if (StringUtils.isNotBlank(location)) {
                String[] array = location.split(" ");
                if (array.length > 0) {
                    address = array[0];
                }
            }
            return address;
        } catch (Exception e) {
            return "";
        }
    }


}
